package com.kai.xadmin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kai.xadmin.entity.User;
import com.kai.xadmin.mapper.UserMapper;
import com.kai.xadmin.service.UserService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author trouble
 * @since 2023-08-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Autowired
	private UserService userService;

	@Autowired
	private UserMapper userMapper;

	/**
	 * 处理用户登录
	 */
	@Override
	public User login(User user) {
		// 根据用户名查找用户
		LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(User::getUsername, user.getUsername());
		User userInfo = userMapper.selectOne(queryWrapper);
		if (ObjectUtils.isEmpty(userInfo)
				|| !(new BCryptPasswordEncoder().matches(user.getPassword(), userInfo.getPassword()))) {
			return null;
		}
//		if (!userInfo.getPassword().equals(user.getPassword())) {
//			return null;
//		}
		userInfo.setPassword(null);
		return userInfo;
	}

}
